﻿// Necroz project team © <2015> <ArchLord2EMU>
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Author:Sagara
// Date:2015-09-06-19:36

using System;
using System.Collections.Generic;
using System.IO;
using ArchLord2Commons.Models;
using ArchLord2Commons.Network;
using ArchLord2Commons.Utils;

namespace AL2Login.Network.Frames.Send
{
    public class SfServerlist : AALNetFrame
    {
        private readonly List<GsInfo> _gameServers;
        private readonly string _token;

        public SfServerlist(List<GsInfo> gameServers, string token)
        {
            if(token.Length > 18 & token.Length < 18)
                throw new Exception("invalid token length");

            _gameServers = gameServers;
            _token = token;
        }
        public override void ProcessWrite()
        {
            PutArray("BF588503"); //unk

            byte[] bodyData;

            using (MemoryStream str = new MemoryStream())
            using (BinaryWriter writer = new BinaryWriter(str))
            {
                writer.WriteS(_token, 36);
                writer.Write("00DC6141ED81046C318BD3B18348A46F274B16E60E71C35A1472CE77A994".ToBytes()); //unk
                writer.Write("0100000000".ToBytes());
                writer.Write("0100000000000000".ToBytes());
                writer.Write("E69100".ToBytes());
                writer.Write((byte)_gameServers.Count);

                foreach (var gameServer in _gameServers)
                {
                    writer.Write((int) gameServer.Id);
                   
                    writer.WriteSs(gameServer.Name, 36);
                    writer.Write("51BA4C6FFB2BED9498EA3C129FE9D7D65ED4C8AC30FEDA47CE13".ToBytes()); //unk
                    writer.Write(gameServer.Characters);
                    writer.Write("051100006765000000".ToBytes()); //server status, todo - check flag`s
                }
                bodyData = str.ToArray();
            }

            PutArray(bodyData);
        }
    }
}

//8e13 bf58850235613038303630616c663538696c626338357a36306c353862667a36303538626364313000032f06c5fd307e9ca2f1be267f4d1ddf925e34c23d2c34c750f49bf16500000000000100000000000000cd560002940100005b004500550052004f00500041005d0020004400650069006d006f00740069006b0000008b6854a7600073a086712dac875405254bce264f9a4d19a06a7e03090100009c650000005b0041004d00450052004900430041005d002000570069006c006c006f00770066006900720065000000a724180e67ac666e7ce230bb7958f3d43fe515b30003010000f19fe94058350d51cab5673685695c548048005b1b6e647b1abb22ecb86f5e886cfa3bdbb5c1422c2386782f838348b186fb5e0b7cc068fdde9ab9c4935677b486ab4e55f19fe94058350d51cab5673685695c548048005b1b6e647b1abb22ecb86f5e886cfa3bdbb5c1422c2386782f838348b186fb5e0b7cc068fdde9ab9c4935677b486ab4e55f19fe94058350d51cab5673685695c548048005b1b6e647b1abb22ecb86f5e886cfa3bdbb5c1422c2386782f838348b186fb5e0b7cc068fdde9ab9c4935677b486ab4e55f19fe94058350d51cab5673685695c548048005b1b6e647b1abb22ecb86f5e886cfa3bdbb5c1422c2386782f838348b186fb5e0b7cc068fdde9ab9c4935677b486ab4e55f19fe94058350d51cab5673685695c548048005b1b6e647b1abb22ecb86f5e886cfa3bdbb5c1422c2386782f838348b186fb5e0b7cc068fdde9ab9c4935677b486ab4e55f19fe94058350d51cab5673685695c548048005b1b6e647b1abb22ecb86f5e886cfa3bdbb5c1422c2386782f838348b186fb5e0b7cc068fdde9ab9c4935677b486ab4e55f19fe94058350d51cab5673685695c548048005b1b6e647b1abb22ecb86f5e886cfa3bdbb5c1422c2386782f838348b186fb5e0b7cc068fdde9ab9c4935677b486ab4e55f19fe94058350d51cab5673685695c548048005b1b6e647b1abb22ecb86f5e886cfa3bdbb5c1422c2386782f838348b186fb5e0b7cc068fdde9ab9c4935677b486ab4e55f19fe94073134984c82460674f8b22e1c20d903ad726a485779326d06ff4f09f95365601c5481256534b93c5977e594a93d90ba29fb21fcf433904e0f8b58deca12c01b92587bc1f056d94f1fc7c489d52e6af56df0f67f56835fdcb2c0489a17bdc23b0aba47fd6ec714d6f479b12e750018c8421c5d0d54cc88ac45c6c308034d47571c01acbb1956838e889659539f5e09b10f6bc9ff90acf6e9145eb395aa8210577c97a288083cd691e77d243a0b440715b46d0d965049fe5339c0ccc0ad519977576b35b5da6e8e0b8beb067769ac5e2e61f367ca6edc9744c8a1d3aa0b4e43f4b145f8fa64a494cc957facacf2a09ab4c3631f8a7896a5a0ed0487997e8091b5d340ea3c55634d6409dd02b3495904302ef03a292fc73a389bac2c87313038af2bea6a5885a6d351f2f40c4dc3e826f2b2a785f7c517c9b5281572a0089bc70501ec61ae3a695575eab2a9fe2fc04a04f50bc90c940b98ee7ad05afcf08dab9804731e21384dfd1aa559d95610de06f0b9c1605c751b9eb1da973d67af12b452dfbc76158c96d79e2ba921c24e2e2cfaf7faae1bbc8144bc7b6fe86ae38583b9c2fbc8001a41fbe647b907d7b31dff36c7e8a54a7af7dd51ae95bc6132f24ce413630466beda4809f3765625062f5c052386b8285c358214988a86d458b8959184676218c78c6c402080539e0bb57460f3faf14b841f7a2d8394185be0de75e5c319678ad90015ba04118b9d57e65afb7b8e76d5732fcead6c29f450f4ce4659d91831e57003f330f162214b4bfb7f37c6795bd419aba44b73d0be98ad89e9ec5efb53f33eee0d2f2a2d960c6b9aca46eb4f69148bb42f5996a1486f77fd48b9566dccd673824c8a7231c19517f0ffb49bbe099858a8df0d82065507de68161752e7c914da3f57c3bf318c2faabf78539e366aac27aaf391c49959fab7d6ff7359deaad141c63aaa5fb15eb10ab7608150c4d13dde1779990d2b005f6af8a81c697920e6b948526327832272d13a868ee8580d3ef16e483e869ed057e9821311a1bbcbfa4a651f65c88207fa7